package demo1;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class Message{
    public String message;
    public String to;
    public String from;

    @Override
    public String toString() {
        return "Message{" +
                "message='" + message + '\'' +
                ", to='" + to + '\'' +
                ", from='" + from + '\'' +
                '}';
    }
}


@SuppressWarnings({"all"})
@WebServlet("/message")
public class Test extends HttpServlet {
    private ObjectMapper objectmapper = new ObjectMapper();

    private List<Message> messagelist = new ArrayList<>();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Message message = objectmapper.readValue(req.getReader(), Message.class);
        System.out.println("请求里面收到的message" + message);
        //接下来我们需要考虑如何进行保存
        //1.存储到内存里面：但是刷新就丢失了
        messagelist.add(message);
        resp.setStatus(200);
        resp.getWriter().write("{ok:true}");
    }
}
